package com.ypan.uninstaller.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ypan.uninstaller.MainApp;
import com.ypan.uninstaller.R;
import com.ypan.uninstaller.widget.CustomWaitingDialog;

public class DialogHelper {

	public static void Toast(int resId, boolean isLong) {
		int duration = isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
		Toast toast = Toast.makeText(MainApp.sMainActivity, resId, duration);
		toast.setGravity(Gravity.BOTTOM, 0, 50);
		toast.show();
	}

	public static AlertDialog showAboutDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				MainApp.sMainActivity);
		builder.setTitle(R.string.title_about);
		View view = LayoutInflater.from(MainApp.sMainActivity).inflate(
				R.layout.dialog_about, null);
		TextView version = (TextView) view.findViewById(R.id.version);
		version.append(" ");
		version.append(MainApp.sMainActivity.getResources().getString(
				R.string.app_name));
		version.append(" ");
		version.append(getVersion());
		builder.setView(view);
		builder.setNeutralButton(MainApp.sMainActivity.getResources()
				.getString(R.string.ok), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});
		return builder.create();
	}

	public interface SortListener {
		public void onSort(int sortType);
	}

	public static AlertDialog showSortDialog(final SortListener sortlistener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				MainApp.sMainActivity);
		builder.setTitle(R.string.title_sort);
		View view = LayoutInflater.from(MainApp.sMainActivity).inflate(
				R.layout.dialog_sort, null);
		builder.setView(view);
		final RadioButton timeDescBtn = (RadioButton) view
				.findViewById(R.id.sort_time_desc);
		final RadioButton timeAscBtn = (RadioButton) view
				.findViewById(R.id.sort_time_asc);
		final RadioButton nameDescBtn = (RadioButton) view
				.findViewById(R.id.sort_name_desc);
		final RadioButton nameAscBtn = (RadioButton) view
				.findViewById(R.id.sort_name_asc);
		int sortType = SharePreferences.getSortType();
		if (Define.SORT_BY_TIME_DESC == sortType) {
			timeDescBtn.setChecked(true);
		} else if (Define.SORT_BY_TIME_ASC == sortType) {
			timeAscBtn.setChecked(true);
		} else if (Define.SORT_BY_NAME_DESC == sortType) {
			nameDescBtn.setChecked(true);
		} else if (Define.SORT_BY_NAME_ASC == sortType) {
			nameAscBtn.setChecked(true);
		}
		DialogInterface.OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (sortlistener == null)
					return;
				if (timeDescBtn.isChecked()) {
					sortlistener.onSort(Define.SORT_BY_TIME_DESC);
				} else if (timeAscBtn.isChecked()) {
					sortlistener.onSort(Define.SORT_BY_TIME_ASC);
				} else if (nameDescBtn.isChecked()) {
					sortlistener.onSort(Define.SORT_BY_NAME_DESC);
				} else if (nameAscBtn.isChecked()) {
					sortlistener.onSort(Define.SORT_BY_NAME_ASC);
				}
			}
		};
		builder.setNeutralButton(MainApp.sMainActivity.getString(R.string.ok),
				listener);
		return builder.create();
	}

	public interface BackupListener {
		public void onBackup(String file, String name, long size);
	}

	public static AlertDialog showBackupDialog(final String file, final String name,
			final long size, final BackupListener backListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				MainApp.sMainActivity);
		builder.setTitle(R.string.title_backup);
		View view = LayoutInflater.from(MainApp.sMainActivity).inflate(
				R.layout.dialog_backup, null);
		builder.setView(view);
		final TextView pathTV = (TextView) view.findViewById(R.id.tv_path);
		pathTV.append(SharePreferences.getBackupPath());
		final TextView nameTV = (TextView) view.findViewById(R.id.tv_name);
		nameTV.append(name + ".apk");
		DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (backListener == null)
					return;
				backListener.onBackup(file, name, size);
			}
		};
		builder.setNeutralButton(MainApp.sMainActivity.getResources()
				.getString(R.string.ok), listener);
		return builder.create();
	}

	public interface UninstallListener {
		public void onClick(int pos,int id);
	}

	public static AlertDialog showUserUninstallDialog(final int pos,final UninstallListener listener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				MainApp.sMainActivity);
		builder.setTitle(R.string.dialog_options);
		View view = LayoutInflater.from(MainApp.sMainActivity).inflate(
				R.layout.dialog_uninstall, null);
		View.OnClickListener clickListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null)
					listener.onClick(pos,v.getId());
			}
		};
		view.findViewById(R.id.uninstall_btn).setOnClickListener(clickListener);
		view.findViewById(R.id.detail_btn).setOnClickListener(clickListener);
		view.findViewById(R.id.start_btn).setOnClickListener(clickListener);
		view.findViewById(R.id.share_btn).setOnClickListener(clickListener);
		view.findViewById(R.id.backup_btn).setOnClickListener(clickListener);
		view.findViewById(R.id.search_btn).setOnClickListener(clickListener);
		builder.setView(view);
		return builder.create();
	}

	public static AlertDialog showSysUninstallDialog(final int pos,final UninstallListener listener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				MainApp.sMainActivity);
		builder.setTitle(R.string.dialog_options);
		View view = LayoutInflater.from(MainApp.sMainActivity).inflate(
				R.layout.dialog_uninstall, null);
		View.OnClickListener clickListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null)
					listener.onClick(pos,v.getId());
			}
		};
		view.findViewById(R.id.uninstall_btn).setOnClickListener(clickListener);
		view.findViewById(R.id.detail_btn).setOnClickListener(clickListener);
		view.findViewById(R.id.start_btn).setOnClickListener(clickListener);
		view.findViewById(R.id.share_btn).setOnClickListener(clickListener);
		view.findViewById(R.id.backup_btn).setOnClickListener(clickListener);
		view.findViewById(R.id.search_btn).setOnClickListener(clickListener);
		builder.setView(view);
		return builder.create();
	}

	public interface InstallListener {
		public void onClick(int pos, int id);
	}

	public static AlertDialog showInstallDialog(final int pos, final InstallListener listener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				MainApp.sMainActivity);
		builder.setTitle(R.string.dialog_options);
		View view = LayoutInflater.from(MainApp.sMainActivity).inflate(
				R.layout.dialog_install, null);
		builder.setView(view);
		final Button installBtn = (Button) view.findViewById(R.id.install_btn);
		final Button deleteBtn = (Button) view.findViewById(R.id.delete_btn);
		View.OnClickListener clickListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null)
					listener.onClick(pos, v.getId());
			}
		};
		installBtn.setOnClickListener(clickListener);
		deleteBtn.setOnClickListener(clickListener);
		return builder.create();
	}

	private static String getVersion() {
		try {
			return MainApp.sMainActivity.getPackageManager().getPackageInfo(
					MainApp.sMainActivity.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			return "version not found";
		}
	}
	
	public static Dialog createWaitingDialog(Context ctx, int strID){
		return new CustomWaitingDialog(ctx,strID);
	}

}
